<?php
/**
 *
 *
 *
 */
class Yz_Form_Element_TinyMCE
    extends Yz_Form_Element_Abstract
{

    const JQUERY_TINYMCE_JS = "./www/tinymce/jscripts/tiny_mce/jquery.tinymce.js";
    const TINYMCE_JS = "www/tinymce/jscripts/tiny_mce/tiny_mce.js";

    protected $name;
    protected $value = "";
    protected $cssClass = "yz-tinymce";


    public function render(Yz_Template_Interface $template)
    {
        $class = $this->getCSSClass();
        $text = htmlspecialchars($this->value);
        $textClick = $template->translate('Click to edit');
        $o = <<<html
            <div class="{$class}">
                <a href="#" onclick="return tinymceClick(this)">{$textClick}</a>
                <textarea name="{$this->name}" class="{$class}">{$text}</textarea>
            </div>\n
html;
        $this->includeJS($template);
        return $o;
    }

    protected function includeJS(/*Yz_Template_Interface*/$t)
    {
        $webRoot = $t->webRoot;
        $tinymceJS = $webRoot . self::TINYMCE_JS;
        $js = <<<js
function tinymceClick(a) {
    $(a).hide()
        .next()
        .show()
        .tinymce({
            script_url : '{$tinymceJS}',
            dummy: false
        });
   return false;
}
js;
        $t->includeJS( $t::JQUERY_JS )
            ->includeJS( self::JQUERY_TINYMCE_JS )
            ->includeJS(
                'tinymce-init',
                $js
            )
        ;
        return $this;
    }


}
